//
//  LoginViewController.m
//  YZZX
//
//  Created by Easaa on 15/5/12.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistrationViewController.h"

//关注频道
#import "FocusChannels.h"

@interface LoginViewController ()<HXHttpRequestDelegate,UITextFieldDelegate>
{
    HXHttpRequest    *_httpRequest;
    UITextField *   _userText;
    UITextField *   _passWordText;
    UIButton        *bottomButton;

}

@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"登录";
    self.view.backgroundColor =RGBCOLOR(233, 233, 233);

    UIView  *LoginViewBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, Dev_ScreenWidth, 80)];
    LoginViewBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:LoginViewBgView];
    
    UIView  *LineViewBgView = [[UIView alloc]initWithFrame:CGRectMake(10, 39, Dev_ScreenWidth-10, 1)];
    LineViewBgView.backgroundColor = RGBCOLOR(233, 233, 233);
    [LoginViewBgView addSubview:LineViewBgView];
    
    
    UILabel *labelTitleUser = [[UILabel alloc] initWithFrame:CGRectMake(10, 0,40, 40)];
    labelTitleUser.font = [UIFont boldSystemFontOfSize:15];         //UILabel的字体大小
    labelTitleUser.numberOfLines = 0;                               //必须定义这个属性，否则UILabel不会换行
    labelTitleUser.textAlignment = NSTextAlignmentLeft;             //文本对齐方式
    labelTitleUser.text =@"账号:";
    [LoginViewBgView addSubview:labelTitleUser];
    
    
    _userText =[[UITextField alloc]initWithFrame:CGRectMake(50, 0, Dev_ScreenWidth-60, 40)];
    _userText.tag =0;
     _userText.delegate =self;
    [_userText becomeFirstResponder];
    _userText.font = [UIFont systemFontOfSize:15];
    [LoginViewBgView addSubview:_userText];
    
    
    UILabel *labelTitleMassge = [[UILabel alloc] initWithFrame:CGRectMake(10, 40,40, 40)];
    labelTitleMassge.font = [UIFont boldSystemFontOfSize:15];           //UILabel的字体大小
    labelTitleMassge.numberOfLines = 0;                                 //必须定义这个属性，否则UILabel不会换行
    labelTitleMassge.textAlignment = NSTextAlignmentLeft;               //文本对齐方式
//    labelTitleMassge.textColor = [UIColor grayColor];
    labelTitleMassge.text =@"密码:";
    [LoginViewBgView addSubview:labelTitleMassge];

    
    
    _passWordText =[[UITextField alloc]initWithFrame:CGRectMake(50, 40, Dev_ScreenWidth-60, 40)];
   // _passWordText.delegate =self;
    _passWordText.tag =0;
    _passWordText.delegate = self;
    _passWordText.font = [UIFont systemFontOfSize:15];
    _passWordText.secureTextEntry = YES;  //密文
   // PassWordText.backgroundColor = [UIColor greenColor];
    [LoginViewBgView addSubview:_passWordText];
    
    
    
    bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.tag = 2014 ;
    bottomButton.frame =CGRectMake(10,LoginViewBgView.bottom+40 ,Dev_ScreenWidth-20, 40);
    [bottomButton addTarget:self action:@selector(RequestHeadlLinesAndHeadImageDataFantion) forControlEvents:UIControlEventTouchUpInside];
    [bottomButton.layer setMasksToBounds:YES];
    [bottomButton.layer setCornerRadius:2]; //设置矩圆角半径
    [bottomButton.layer setBorderWidth:0];   //边框宽度
    
    [bottomButton setTitle:@"登录" forState:UIControlStateNormal];
    [bottomButton setTintColor:[UIColor whiteColor]];
    bottomButton.backgroundColor =RGBCOLOR(252, 78, 125);
    [self.view addSubview:bottomButton];
    
    
    UIButton *ButRegistration = [UIButton buttonWithType:UIButtonTypeCustom];
    ButRegistration.tag = 2014 ;
    ButRegistration.frame =CGRectMake(Dev_ScreenWidth-20-50,bottomButton.bottom ,60, 40);
    [ButRegistration addTarget:self action:@selector(ButRegistrationUser) forControlEvents:UIControlEventTouchUpInside];
    [ButRegistration setTitle:@"注册账号" forState:UIControlStateNormal];
    
    ButRegistration.titleLabel.textAlignment = NSTextAlignmentRight;
    ButRegistration.titleLabel.font = [UIFont systemFontOfSize:12];
    ButRegistration.titleLabel.textColor = [UIColor blackColor];
     [ButRegistration setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    //ButRegistration.backgroundColor =RGBCOLOR(252, 78, 125);
    [self.view addSubview:ButRegistration];

    
    
    
    //一个通知 栏目跳转
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zhucetongzhi)name:YZZX_Login_zhuce_pop object:nil];

    
#pragma mark Test账号

//    _userText.text=@"DeviceApiTester";
//    _passWordText.text =@"c8z4mb78";

}

- (void)zhucetongzhi
{
    
    NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
    _userText.text = [UserDefaultSize objectForKey:YZZX_Login_name_zhuce ];
    _passWordText.text =[UserDefaultSize objectForKey:YZZX_Login_password_zhuce];
    
}

- (void)ButRegistrationUser
{

    RegistrationViewController  *registrationView =[[RegistrationViewController alloc]init];
    [self.navigationController pushViewController:registrationView animated:YES];

}

-(void)RequestHeadlLinesAndHeadImageDataFantion
{
    if(_passWordText.text.length>0&&_userText.text.length>0)
    {
//        用户名：DeviceApiTester
//        密码：c8z4mb78
        
        
        //列表请求  参数为 用户ID  、 用户账号
        NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                                   _userText.text,                  @"user",
                                   _passWordText.text,              @"password",
                                   @"1",                            @"device_id",
   //                               @"1",                    @"mobile",
                                   
                                   nil];
        
        if (_httpRequest == nil)
        {
            _httpRequest = [[HXHttpRequest alloc] init];
            _httpRequest.delegate = self;
            
            
            
        }
        [_httpRequest requestForParams:dicParams andMethod:YZ_MEMBER_LOGIN andTag:1001];
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    }else
    {
    
        
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入完整！"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1024;
        [AlertView show];
    
    
    }
    
    
}



#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
  [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
    NSDictionary *dic = [[NSDictionary alloc]init];
    dic = [JsonUtil jsonToDic:responseString];
    
    if ([dic[@"data"] isEqual:[NSNull null]])
    {
        return;
    }
    
    if(![dic[@"is_success"] boolValue])
    {
    
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:[dic objectForKey:@"message"]
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];

        AlertView.tag = 1028;
        [AlertView show];
        return;
    }
    
    NSString * strs = dic[@"data"];
    
    NSDictionary *dics =[JsonUtil jsonToDic:strs];
    
    NSLog(@"---------->%@",dics);
    
    //设置//用户名
    NSUserDefaults *UserDefaultSize =[NSUserDefaults standardUserDefaults];
    [UserDefaultSize setObject:[dics objectForKey:@"fullname"] forKey:YZZX_Login_fullname];
    
    //设置//用户id

   [UserDefaultSize setObject:[[dics objectForKey:@"user_id"] stringValue] forKey:YZZX_Login_user_id];
    
    //设置//站点id

    [UserDefaultSize setObject:[[dics objectForKey:@"site_id"] stringValue] forKey:YZZX_Login_site_id];
   
 
   
    //创建通知
    NSNotification *notification =[NSNotification notificationWithName:YZZX_Login_Bool_Yes object:nil userInfo:nil];
    //通过通知中心发送通知
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    
    if([UserDefaultSize integerForKey:YZZX_guanzhu_pindao])
    {
       [self.navigationController popViewControllerAnimated:YES];

    }else
    {
        
        FocusChannels  *Viewspush =[[FocusChannels alloc]init];
        Viewspush.delegatViewroot = self.Viewdegat;
        [self.navigationController pushViewController:Viewspush animated:YES];
        
        
    }
    
    
    
    
    
}


#pragma mark  HXHttpRequest - 请求代理，，错误代理
- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
    
    
    
    
}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    [theTextField resignFirstResponder];
    
    return YES;
}



#pragma mark - 限制密码输入6位，手机号输入11位

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if(_userText.text.length<=0||_passWordText.text.length<=0)
//    {
//        
//        [bottomButton setEnabled:NO];
//        
//    }else if (_userText.text.length>0&&_passWordText.text.length>0)
//    {
//    
//         [bottomButton setEnabled:YES];
//    
//    
//    }
//        return YES;
//}
//
//#pragma mark - 控制“获取验证码”按钮
//- (void)textFieldDidEndEditing:(UITextField *)textField
//{
//    if(_userText.text.length<=0||_passWordText.text.length<=0)
//    {
//        
//        [bottomButton setEnabled:NO];
//        
//    }else if (_userText.text.length>0&&_passWordText.text.length>0)
//    {
//        
//        [bottomButton setEnabled:YES];
//        
//        
//    }
//
//}
//
@end
